package capricornus.net.loader.multiloader
{
	import capricornus.events.LoaderEvent;
	import capricornus.events.ResLoaderEvent;
	import capricornus.net.NetWorkManager;
	import capricornus.net.loader.ResLoader;
	import capricornus.net.loader.SingleUrlLoader;
	import capricornus.net.loader.multiloader.event.MultiLoadEvent;
	import capricornus.utils.StringUtils;
	
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	[Event(name="beginload", type="capricornus.net.loader.multiloader.event.MultiLoadEvent")]
	[Event(name="onecomplete", type="capricornus.net.loader.multiloader.event.MultiLoadEvent")]
	[Event(name="oneerror", type="capricornus.net.loader.multiloader.event.MultiLoadEvent")]
	[Event(name="loadProgressProportion", type="capricornus.net.loader.multiloader.event.MultiLoadEvent")]
	[Event(name="allcomplete", type="capricornus.net.loader.multiloader.event.MultiLoadEvent")]
	public class MultiLoader extends EventDispatcher
	{
		private var _singleURLLoader:SingleUrlLoader;
		
		private var _assets:Array;
		
		private var _currentLoadIndex:int = 0;
		private var _dictContent:Dictionary;
		
		private var _dictLoader:Dictionary;
		
		public function MultiLoader()
		{
			super(null);
			init();
		}
		
		public static var parseFun:Function=null;
		
		private function init():void
		{
			_singleURLLoader = new SingleUrlLoader();
			_singleURLLoader.addEventListener( LoaderEvent.LOAD_COMPLETE, onLoaded, false, 0, true );
			_singleURLLoader.addEventListener( LoaderEvent.LOAD_PROGRESS_PROPORTION, onLoadProgressProportion, false, 0, true );
			
			_dictLoader = new Dictionary();
			_dictContent = new Dictionary();
		}
		
		public function destroy():void
		{
			_assets = null;
			
			_singleURLLoader.removeEventListener( LoaderEvent.LOAD_COMPLETE, onLoaded );
			_singleURLLoader.removeEventListener( LoaderEvent.LOAD_PROGRESS_PROPORTION, onLoadProgressProportion );
			_singleURLLoader.destroy();
			
			var i:*;
			for( i in _dictContent )
			{
				delete _dictContent[i];
			}
			for( i in _dictLoader )
			{
				var l:ResLoader = _dictLoader[i];
				if( l != null )
				{
					l.destroy();
				}
				delete _dictLoader[i];
			}
			_dictLoader = null;
			_dictContent = null;
			parseFun = null;
		}
		
		public function loadAssets( assets:Array ):void
		{
			if( assets && assets.length>0 )
			{
				_assets = assets;
				_currentLoadIndex = 0;
				_oneLoaded = 0;
				loadNext();
			}
		}
		
		public function getContentByUrl( url:String ):*
		{
			url = StringUtils.clearHttpReqVars( url );
			return _dictContent[url];
		}
		
		private function loadNext():void
		{
			if( _assets && _currentLoadIndex < _assets.length )
			{
				dispatchEvent( new MultiLoadEvent( MultiLoadEvent.BEGIN_LOAD, _currentLoadIndex ) );
				_singleURLLoader.load( _assets[_currentLoadIndex] );
			}
			/*else
			{
				setTimeout( callLatter, 300 );
			}*/
			
			_currentLoadIndex++;
		}
		
		private function callLatter():void
		{
			dispatchEvent( new MultiLoadEvent( MultiLoadEvent.ALL_COMPLETE ) );
		}
		
		private function onLoaded( e:LoaderEvent ):void
		{
			var byte:ByteArray = e.data as ByteArray;
			var url:String = _singleURLLoader.url;
			url = StringUtils.clearHttpReqVars( url );
			if( byte )
			{
				var substr:String = url.substr( url.length-3 ).toLowerCase();
				if( substr=="swf" || substr=="png" || substr=="jpg" )
				{
					var sl:ResLoader = new ResLoader();
					_dictLoader[sl] = sl;
					sl.addEventListener( ResLoaderEvent.COMPLETE, onByteLoaded );
					sl.loadBytes( byte, new LoaderContext( false, ApplicationDomain.currentDomain ) );
				}
				else if( substr=="xxx" )
				{
					_dictContent[url] = byte;
					if( parseFun != null )
					{
						parseFun( url, byte );
					}
					
					this.oneLoaded++;
				}
				else
				{
					_dictContent[url] = byte
					this.oneLoaded++;
				}
			}
			else
			{
				trace( "装载失败：", url );
				_dictContent[url] = byte;
				this.oneLoaded++;
			}
			
			dispatchEvent( new MultiLoadEvent( MultiLoadEvent.ONE_COMPLETE, url ) );
			loadNext();
		}
		
		private var _oneLoaded:int = 0;
		private function set oneLoaded( value:int ):void
		{
			_oneLoaded = value;
			
			if( value==_assets.length )
			{
				callLatter();
			}
		}
		
		private function get oneLoaded():int
		{
			return _oneLoaded;
		}
		
		private function onByteLoaded( e:ResLoaderEvent ):void
		{
			var sl:ResLoader = e.currentTarget as ResLoader;
			sl.removeEventListener( ResLoaderEvent.COMPLETE, onByteLoaded );
			delete _dictLoader[sl];
			
			_dictContent[StringUtils.clearHttpReqVars( sl.url )] = e.data;
			
			sl.destroy();
			
			this.oneLoaded++;
		}
		
		private function onLoadProgressProportion( e:LoaderEvent ):void
		{
			dispatchEvent( new MultiLoadEvent( MultiLoadEvent.LOAD_PROGRESS_PROPORTION, e.data ) );
		}
	}
}




